package com.example.dell.startproject_225zxx.model;


import com.example.dell.startproject_225zxx.Service.Weather51Service;
import com.example.dell.startproject_225zxx.bean.WeatherResult;
import com.example.dell.startproject_225zxx.iface.WeatherListener;
import com.example.dell.startproject_225zxx.iface.Weatheriface;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dell on 2017/3/14.
 */

public class Weather51Model implements Weatheriface{
    private Retrofit retrofit;
    private String BASEURL="http://weather.51wnl.com/weatherinfo/";
    private Weather51Service weather51Service;
    public Weather51Model() {
        //Retrofit使用3-18
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getWeather(String cityNumber,final WeatherListener listener) {
        //Retrofit使用3-2
        weather51Service=retrofit.create(Weather51Service.class);
        Call<WeatherResult> call=weather51Service.getResult(cityNumber,1);
        //Retrofit使用3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body().getWeatherInfo());
                else
                    listener.onFail("onresponse fail");

            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }
}
